package haitao.photobrowser.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.haitao.glide.GlideImageLoader;
import com.haitao.glide.progress.CircleProgressView;
import com.haitao.glide.progress.OnProgressListener;

import java.util.ArrayList;
import java.util.List;

import haitao.library.photobrowser.R;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

public class PhotoViewActivity extends Activity {

    private ViewPager vp;
    private TextView indexTV;
    private TextView indexContentTV;
    /**
     * url 地址列表 ArrayList
     */
    public static final String URL_LIST = "url_list";
    /**
     * url 地址列表 String []
     */
    public static final String URL_ARRAY = "url_array";

    /**
     * 标题列表 String []
     */
    public static final String TITLE_ARRAY_LIST = "title_list";

    /**
     * 标题列表   ArrayList
     */
    public static final String TITLE_ARRAY = "title_array";

    /**
     * 引索
     */
    public static final String INDEX = "index";
    /**
     * 引索
     */
    public static final String USE_PROGRESS_VIEW = "useProgressView";

    /**
     * 图片地址 ArrayList
     */
    private List<String> imageUrlsList;
    /**
     * 图片地址 String []
     */
    private String[] imageUrlsArray;

    /**
     * 标题 数组 ArrayList
     */
    private List<String> titleList;

    /**
     * 标题 数组 String[]
     */
    private String titlesArray[];

    private boolean isTitleList = true;

    private boolean isUrlList = true;

    private boolean useProgressView = true;


    ArrayList<String> list = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_photo_view);
        vp = (ViewPager) findViewById(R.id.viewPager);
        indexTV = (TextView) findViewById(R.id.indexTV);
        indexContentTV = (TextView) findViewById(R.id.indexContentTV);

        Intent intent = getIntent();
        imageUrlsList = intent.getStringArrayListExtra(URL_LIST);
        imageUrlsArray = intent.getStringArrayExtra(URL_ARRAY);
        titleList = intent.getStringArrayListExtra(TITLE_ARRAY);
        titlesArray = intent.getStringArrayExtra(TITLE_ARRAY_LIST);

        if (titlesArray != null) {
            isTitleList = false;
        }
        if (imageUrlsArray != null) {
            isUrlList = false;
        }
        if (isUrlList) {
            list.addAll(imageUrlsList);
        } else {
            for (int i = 0; i < imageUrlsArray.length; i++) {
                list.add(imageUrlsArray[i]);
            }
        }
        if (list.size() <= 1) {
            indexTV.setVisibility(View.GONE);
        }
        int index = getIntent().getIntExtra(INDEX, 0);
        vp.setAdapter(new CustomPagerAdapter(this));
        useProgressView = getIntent().getBooleanExtra(USE_PROGRESS_VIEW, true);
        vp.setCurrentItem(index);
        updateStatus(index);

        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                updateStatus(position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    class CustomPagerAdapter extends PagerAdapter {

        Context mContext;
        LayoutInflater mLayoutInflater;

        public CustomPagerAdapter(Context context) {
            mContext = context;
            mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == ((FrameLayout) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {

            View itemView = mLayoutInflater.inflate(R.layout.fragment_photo_view, container, false);
            ((ViewPager) container).addView(itemView);
            final PhotoView imageView = (PhotoView) itemView.findViewById(R.id.photoIm);
            final CircularProgressBar progressBar = (CircularProgressBar) itemView.findViewById(R.id.progressBar);
            final CircleProgressView progressView = (CircleProgressView) itemView.findViewById(R.id.progressView);
            progressBar.setVisibility(!useProgressView ? View.VISIBLE : View.GONE);
            progressView.setVisibility(useProgressView ? View.VISIBLE : View.GONE);
            String url = list.get(position);

            imageView.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
                @Override
                public void onViewTap(View view, float x, float y) {
                    finish();
                }
            });
            imageView.setBackgroundResource(0);
            GlideImageLoader.create(imageView)
                    .listener(new OnProgressListener() {
                        @Override
                        public void onProgress(int state, int percentage) {
                            if (state == LOAD_ING) {
                                if (useProgressView) {
                                    progressView.setProgress(percentage);
                                } else {
                                    progressBar.setProgressPecentage(percentage * 0.01f);
                                }
                            } else {
                                progressBar.setVisibility(View.GONE);
                                progressView.setVisibility(View.GONE);
                                if (state == LOAD_FAIL) {
                                    imageView.setBackgroundResource(R.drawable.loadfail);
                                }
                            }
                        }
                    })
                    .centerInside()
                    .placeholder(R.color.placeholder)
                    .loadImage(url);

            return itemView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((FrameLayout) object);
        }
    }


    public void updateStatus(int position) {


        if (isUrlList) {

            this.indexTV.setText(position + 1 + "/" + imageUrlsList.size());
        } else {
            this.indexTV.setText(position + 1 + "/" + imageUrlsArray.length);
        }

        if (isTitleList) {
            if (titleList != null && titleList.size() != 0) {

                if (position < titleList.size()) {

                    this.indexContentTV.setText(titleList.get(position));
                } else {
                    this.indexContentTV.setText(titleList.get(titleList.size() - 1));
                }
            } else {
                this.indexContentTV.setVisibility(View.GONE);
            }
        } else {
            if (titlesArray != null && titlesArray.length != 0) {

                if (position < titlesArray.length) {

                    this.indexContentTV.setText(titlesArray[position]);
                } else {
                    this.indexContentTV.setText(titlesArray[titlesArray.length - 1]);
                }
            } else {
                this.indexContentTV.setVisibility(View.GONE);
            }
        }

    }


}
